import Vue from 'vue'
import router from './router'
import store from './store'

import {ACCESS_TOKEN, DRAG_TOKEN} from '@/store/mutation-types'
import cookies from "vue-cookies";


router.beforeEach((to, from, next) => {
    if(to.name === 'login' || to.name === 'register' || to.name === 'view'){  //如果是单点登录路由或者 预览路由，则不执行获取用户信息操作
        next()
        return
    }
    const token = cookies.get(DRAG_TOKEN)
    if(!token){
        next({name: "login"})
    }
    if (!store.getters.userInfo&& token) {
        store.dispatch('GetUserInfo').then(res => {
            if (res.data.code === 0) {
                console.log("获取用户信息成功:",res)
            } else {
                console.log("获取用户信息失败！")
            }
        }).catch(error => {
            console.log("获取用户信息失败！", error)
        })
    }
    next()

})
